tableData = function (table_id) {
	this.libPath = '/cms/scripts/';
	this.table = this._get(table_id);
	this.widget = {};

	this.widget['html'] = function(params) {
		var divEl = document.createElement('DIV');
		divEl.className = 'icon';

		var imgEl = document.createElement('IMG');
		imgEl.src = '/cms/images/' + (params['value']=='0' ? 'document_plus.png' : 'document_text.png');

		var aEl = document.createElement('A');
		aEl.href = 'javascript:;';
		aEl.onclick = function () {
				loadContent(params['id'],'html',function () {
						dojo.xhrGet({
							url: "/cms/html/filled/id/"+params['id']+"/",
							load: function(response, ioArgs) {
							  imgEl.src = '/cms/images/' + (parseInt(response) == 1 ? 'document_text.png' : 'document_plus.png'); 
							  return response;
							},
							error: function(response, ioArgs){
							  alert("An error occurred, with response: " + response);
							  return response;
							},
							handleAs: "text",
							sync: true
						  });
					});
			}

		divEl.appendChild(aEl);
		aEl.appendChild(imgEl);

		return divEl;
	};

	this.widget['plaintext'] = function(params) {
		var divEl = document.createElement('DIV');
		divEl.className = 'icon';

		var imgEl = document.createElement('IMG');
		imgEl.src = '/cms/images/' + (params['value']=='0' ? 'document_plus.png' : 'document_text.png');

		var aEl = document.createElement('A');
		aEl.href = 'javascript:;';
		aEl.onclick = function () {
				loadContent(params['id'],'plaintext',function () {
						dojo.xhrGet({
							url: "/cms/html/filled/id/"+params['id']+"/",
							load: function(response, ioArgs) {
							  imgEl.src = '/cms/images/' + (parseInt(response) == 1 ? 'document_text.png' : 'document_plus.png'); 
							  return response;
							},
							error: function(response, ioArgs){
							  alert("An error occurred, with response: " + response);
							  return response;
							},
							handleAs: "text",
							sync: true
						  });
					});
			}

		divEl.appendChild(aEl);
		aEl.appendChild(imgEl);

		return divEl;
	};
	
	this.widget['cover'] = function(params) {
		var divEl = document.createElement('DIV');
		divEl.className = 'icon';

		var imgEl = document.createElement('IMG');
		imgEl.src = '/cms/images/' + (params['value']=='0' ? 'image_plus.png' : 'image.png');

		var aEl = document.createElement('A');
		aEl.href = 'javascript:;';
		aEl.onclick = function () {
				loadContent(params['id'],'cover',function () {
						dojo.xhrGet({
							url: "/cms/cover/filled/id/"+params['id']+"/",
							load: function(response, ioArgs) {
							  imgEl.src = '/cms/images/' + (parseInt(response) == 1 ? 'image.png' : 'image_plus.png'); 
							  return response;
							},
							error: function(response, ioArgs){
							  alert("An error occurred, with response: " + response);
							  return response;
							},
							handleAs: "text",
							sync: true
						  });
					});
			}

		divEl.appendChild(aEl);
		aEl.appendChild(imgEl);

		return divEl;
	};

	this.widget['gallery'] = function(params) {
		var divEl = document.createElement('DIV');
		divEl.className = 'icon';

		var imgEl = document.createElement('IMG');
		imgEl.src = '/cms/images/' + (params['value']=='0' ? 'images_plus.png' : 'images.png');

		var aEl = document.createElement('A');
		aEl.href = 'javascript:;';
		aEl.onclick = function () {
				loadContent(params['id'],'gallery',function () {
						dojo.xhrGet({
							url: "/cms/gallery/filled/id/"+params['id']+"/",
							load: function(response, ioArgs) {
							  imgEl.src = '/cms/images/' + (parseInt(response) == 1 ? 'images.png' : 'images_plus.png'); 
							  return response;
							},
							error: function(response, ioArgs){
							  alert("An error occurred, with response: " + response);
							  return response;
							},
							handleAs: "text",
							sync: true
						  });
					});
			}		

		divEl.appendChild(aEl);
		aEl.appendChild(imgEl);

		return divEl;
	};	
	
	this.widget['bannerlist'] = function(params) {
		var divEl = document.createElement('DIV');
		divEl.className = 'icon';

		var imgEl = document.createElement('IMG');
		imgEl.src = '/cms/images/' + (params['value']=='0' ? 'blogs_plus.png' : 'blogs.png');

		var aEl = document.createElement('A');
		aEl.href = 'javascript:;';
		aEl.onclick = function () {
				loadContent(params['id'],'bannerlist',function () {
						dojo.xhrGet({
							url: "/cms/bannerlist/filled/id/"+params['id']+"/",
							load: function(response, ioArgs) {
							  imgEl.src = '/cms/images/' + (parseInt(response) == 1 ? 'blogs.png' : 'blogs_plus.png'); 
							  return response;
							},
							error: function(response, ioArgs){
							  alert("An error occurred, with response: " + response);
							  return response;
							},
							handleAs: "text",
							sync: true
						  });
					});
			}		

		divEl.appendChild(aEl);
		aEl.appendChild(imgEl);

		return divEl;
	};
	
	this.widget['comments'] = function(params) {
            var divEl = document.createElement('DIV');
			var aEl = document.createElement('A');
			aEl.href = 'javascript:;';
			var imgEl = document.createElement('IMG');
			if( params['value'] == 0 )
                imgEl.src = '/cms/images/balloons.png';
            else
                imgEl.src = '/cms/images/balloons_exclamation.png';
			
			aEl.onclick = function () {
                    shadowbox_callback = function(){
						dojo.xhrGet({
							url: "/cms/comments/geticon/id/"+params['id']+"/",
							load: function(response, ioArgs) {
							  if( response > 0 )
							     imgEl.src = '/cms/images/balloons_exclamation.png'; 
							  else
							     imgEl.src = '/cms/images/balloons.png'; 
							  
							  return response;
							},
							error: function(response, ioArgs){
							  alert("An error occurred, with response: " + response);
							  return response;
							},
							handleAs: "text",
							sync: true
						  });
                    }
					Shadowbox.open({
					    player:     'iframe',
					    title:      'Комментарии к товару',
					    content:    "/cms/comments/index/comment_id/" + params['id'],
					    height:     450,
					    width:      750
					});
				}

			divEl.className = 'icon';
			aEl.appendChild(imgEl);
			
			divEl.appendChild(aEl);
            return divEl;  
        }
	
	this.widget['rating'] = function(params){
        var divEl = document.createElement('DIV');
        divEl.className = 'icon';       
		var aEl = document.createElement('A');

		var imgEl = document.createElement('IMG');
		imgEl.src = '/cms/images/crown.png';
		imgEl.title = params['value'];

		aEl.href = 'javascript:;';
		aEl.onclick = function () {
                shadowbox_callback = function(){
					dojo.xhrGet({
						url: "/cms/rating/getrating/id/"+params['id']+"/",
						load: function(response, ioArgs) {
                            imgEl.title = response; 
						  return response;
						},
						error: function(response, ioArgs){
						  alert("An error occurred, with response: " + response);
						  return response;
						},
						handleAs: "text",
						sync: true
					  });
                }
                			
				Shadowbox.open({
				    player:     'iframe',
				    title:      'Рейтинг товара',
				    content:    "/cms/rating/rating/rating_id/" + params['id'],
				    height:     250,
				    width:      400
				});
			}			
		
		aEl.appendChild(imgEl);            
        divEl.appendChild(aEl);			
        return divEl;  
    }
	
	this.widget['formsender'] = function(params){
        var divEl = document.createElement('DIV');
        divEl.className = 'icon';       
		var aEl = document.createElement('A');

		var imgEl = document.createElement('IMG');
		imgEl.src = '/cms/images/crown.png';
		imgEl.title = params['value'];

		aEl.href = 'javascript:;';
		aEl.onclick = function () {
				loadContent(params['id'],'formsender',function () {});
			};			
		
		aEl.appendChild(imgEl);            
        divEl.appendChild(aEl);			
        return divEl;  
    }
/*        
	this.widget['comments'] = function(params) {
            var divEl = document.createElement('DIV');
			var aEl = document.createElement('A');
			aEl.href = 'javascript:;';
			aEl.onclick = function () {
					Shadowbox.open({
					    player:     'iframe',
					    title:      'Комментарии к товару',
					    content:    "/cms/comments/index/comment_id/" + params['id'],
					    height:     450,
					    width:      750
					});
				}

			divEl.className = 'icon';
			var imgEl = document.createElement('IMG');
			if( params['value'] == 0 )
                imgEl.src = '/cms/images/balloons.png';
            else
                imgEl.src = '/cms/images/balloons_exclamation.png';
			aEl.appendChild(imgEl);
			
			divEl.appendChild(aEl);
            return divEl;  
        }	*/
}

tableData.prototype._get = function (obj) {
	if (typeof obj == 'string') {
		return document.getElementById(obj);
	}
	return obj;
}

tableData.prototype.draw = function (obj) {
	this.clear();

	var hightlight_f = false;

	for (var r=0;r<obj.length;r++) {
		var row = document.createElement('TR');

		if (!hightlight_f) {
			hightlight_f = true;
		} else {
			row.className = 'highlight';
			hightlight_f = false;
		}

		for (var t=0;t<obj[r].length;t++) {
			var cell = document.createElement('TD');
			var cellWidget = typeof this.widget[obj[r][t][0]]=='function' ? this.widget[obj[r][t][0]] : 
				function (a) { 
					var n = document.createElement('div'); 
					n.innerHTML = '&lt;no widget&gt;'; 
					return n; 
					};
			cell.appendChild(cellWidget(obj[r][t][1]));
			row.appendChild(cell);
		}
		this.table.appendChild(row);
	}
}
 
tableData.prototype.clear = function () {
	while (this.table.childNodes.length>0) {
		this.table.removeChild(this.table.childNodes.item(0));
	}
}
 
tableData.prototype.registerWidget = function(widget_name,widget_object) {
	this.widget[widget_name] = widget_object;
}